package com.weetop.akly.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.weetop.akly.R;
import com.weetop.akly.activity.login.LoginActivity;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BasePresenter;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.utils.MMKVUtils;
import com.weetop.akly.utils.MyDataCleanManager;
import com.weetop.akly.utils.widget.AlertDialogIOS;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imkit.userInfoCache.RongUserInfoManager;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_data_title)
    TextView tvDataTitle;
    @BindView(R.id.rl_aboutus)
    RelativeLayout rlAboutus;
    @BindView(R.id.rl_exit)
    RelativeLayout rlExit;
    @BindView(R.id.tv_cache_size)
    TextView tvCacheSize;
    @BindView(R.id.rl_clear)
    RelativeLayout rlClear;
    private AlertDialogIOS myDialog;
    private String totalCacheSize = "";

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        setTitleInfo("通用设置");
        initAletDialog();
        initCache();
    }


    private void initAletDialog() {
        myDialog = new AlertDialogIOS(this).builder();
    }


    private void initCache() {
        try {
            totalCacheSize = MyDataCleanManager.getTotalCacheSize(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvCacheSize.setText(totalCacheSize);
    }

    @OnClick({R.id.rl_aboutus, R.id.rl_exit, R.id.rl_clear})
    public void onViewClicked(View view) {
        if (BaseUtils.fastClick())
            switch (view.getId()) {
                case R.id.rl_aboutus:
                    BaseUtils.toActivity(mActivity, AboutUsActivity.class);
                    break;
                case R.id.rl_clear:
                    if (TextUtils.isEmpty(totalCacheSize) || totalCacheSize == "0.00KB") {
                        return;
                    }
                    myDialog.setGone().setTitle("提示").setMsg("是否确定清除本地缓存？").setNegativeButton("取消", null).setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            boolean b = MyDataCleanManager.clearAllCache(mActivity);
                            if (b) {
                                ToastUtils.showShort("成功清除" + totalCacheSize + "缓存");
                                initCache();
                            }
                        }
                    }).show();
                    break;
                case R.id.rl_exit:
                    myDialog.setGone().setTitle("提示").setMsg("是否确定退出当前账户？").setNegativeButton("取消", null).setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MMKVUtils.removeUserToken();
                            MMKVUtils.removeRyToken();
                            RongIM.getInstance().logout();
                            RongUserInfoManager.getInstance().uninit();
                            Intent intent = new Intent(mActivity, LoginActivity.class);
                            startActivity(intent);
                            ActivityUtils.finishAllActivitiesExceptNewest();
                            mActivity.finish();
                        }
                    }).show();
                    break;
            }
    }
}
